package com.it.gw.modules.station.activity

import android.annotation.SuppressLint
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.DateUtils
import com.it.common.utils.DisplayUtil
import com.it.common.utils.RvSpacesItemUtils
import com.it.data.network.rsp.WrongRecVo
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityEquipmentFailureStatisticsBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.adapter.EqStaticAdapter


//设备故障统计
class EquipmentFailureStatisticsActivity :
    BaseActivity<ActivityEquipmentFailureStatisticsBinding>() {

    companion object {
        const val PAGE_SIZE = 20
        const val TAG = "EquipmentFailureStatisticsActivity"
    }

    private var pageNum = 1
    private val datas by lazy { mutableListOf<WrongRecVo>() }
    private val adapter by lazy {
        EqStaticAdapter(context = this, datas = datas, listener = {

        })
    }

    override fun initObserver() {

        gqStationViewModel.wrongRecVoListLiveData.observe(this) {
            ProgressDialog.hide()
            loadMoreData = false
            binding.swipeRefreshLayout.isRefreshing = false
            if(it.result)
            { if (it.data?.list.isNullOrEmpty()) {
                    if (pageNum > 1) {
                        pageNum--
                        Toast.makeText(this, "没有更多数据了！", Toast.LENGTH_SHORT).show()
                    } else {
                        //第一次就没数据
                        datas.clear()
                        adapter.notifyDataSetChanged()
//                    llNoData.setVisibility(View.VISIBLE);
//                    srHasData.setVisibility(View.GONE);
                    }

                } else {
//                llNoData.setVisibility(View.GONE);
//                srHasData.setVisibility(View.VISIBLE);
                    if (pageNum == 1) {
                        datas.clear()
                    }
                    datas.addAll(it.data!!.list!!)
                    Log.d(TAG, "initObserver: wrongRecVoListLiveData ${datas}")
                    adapter.notifyDataSetChanged()

                }

            }
            else{
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_LONG).show()
            }

        }

    }


    @SuppressLint("RestrictedApi")
    override fun initListener() {
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.searchContent.setText(null)
            pageNum = 1
            getData()
        }
        binding.rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                // 获取布局管理器
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager

                // 获取最后一个可见的 item 的位置
                val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()

                // 获取总的 item 数量
                val totalItemCount = layoutManager.itemCount

                // 判断是否滑动到了底部
                if (totalItemCount >= PAGE_SIZE && lastVisibleItemPosition == totalItemCount - 1) {
                    // 滑动到了底部，执行相关操作
                    loadMoreData()
                }
            }
        })
        binding.startTime.setOnClickListener {
            showDatePickerDialog {
                binding.startTime.text = it
            }
        }
        binding.endTime.setOnClickListener {
            showDatePickerDialog {
                binding.endTime.text = it
            }
        }
        binding.btnOk.setOnClickListener {
            ProgressDialog.show(this)
            pageNum=1
            getData()
        }

    }


    private var loadMoreData = false;
    private fun loadMoreData() {
        if(loadMoreData){
            return
        }
        Log.d(TAG, "loadMoreData: ")
        pageNum += 1
        loadMoreData = true;
        getData()
    }

    override fun initData() {
        ProgressDialog.show(this)
        getData()

    }


    private fun getData() {
        gqStationViewModel.getWrongRec(
            startTime = binding.startTime.text.toString(),
            endTime = binding.endTime.text.toString(),
            pageSize = PAGE_SIZE,
            pageNo = pageNum,
            equModel = binding.searchContent.text.toString()
        )


    }

    override fun initView() {
        setTitle(
            title = R.string.str_eq_static,
            isBack = true,
        )

        val map = hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to DisplayUtil.dip2px(this,8f))
        binding.rvList.addItemDecoration(RvSpacesItemUtils(map))
        binding.rvList.adapter = adapter
        binding.startTime.text = DateUtils.getFirstDayOfYear()
        binding.endTime.text = DateUtils.getToday()

    }

    override fun setContentView() {
        binding = ActivityEquipmentFailureStatisticsBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}